Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What will be the output of the following Cpro... Start Learning for Free
What will be the output of the following C program?
void count (int n) {
static int d=1;
printf ("%d",n);
printf ("%d",d);
 d++;
if (n>1) count (n-1);
 printf ("%d",d);
}
void main(){
 count (3);
}
  • a)
    3 1 2 2 1 3 4 4 4
  • b)
    3 1 2 1 1 1 2  2 2
  • c)
    3 1 2 2 1 3  4
  • d)
    3 1 2 1 1 1  2
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
What will be the output of the following Cprogram?void count (int n) {...
Count(3) will print value of n and d. So 3 1 will be printed 
and d will become 2. 

Then count(2) will be called. It will print value of n and d. 
So 2 2 will be printed and d will become 3. 

Then count(1) will be called. It will print value of n and d.
So 1 3 will be printed and d will become 4. 

Now count(1) will print value of d which is 4. count(1) will 
finish its execution. 

Then count(2) will print value of d which is 4. 

Similarly, count(3) will print value of d which is 4. 
So series will be A.

View all questions of this test
Most Upvoted Answer
What will be the output of the following Cprogram?void count (int n) {...
Explanation:

The given program is a recursive function that counts down from a given number to 1 and prints the number and a static variable d at each step.

Let's go through the program step by step:

1. The program starts by calling the function 'count' from the 'main' function with the argument 3.

2. Inside the 'count' function, there is a static variable 'd' initialized to 1.

3. The first printf statement prints the value of 'n', which is 3 in the first call.

4. The second printf statement prints the value of 'd', which is 1.

5. The third printf statement prints the value of 'd' again.

6. Then, there is an if condition to check if 'n' is greater than 1. If it is, the 'count' function is called recursively with the argument 'n-1'.

7. In the recursive call, the process repeats. The value of 'n' decreases by 1, and the value of 'd' remains the same.

8. After the recursive call, the last printf statement prints the value of 'd' again.

9. This process continues until 'n' becomes 1.

10. Once 'n' becomes 1, the if condition fails, and the program moves to the next line after the if statement.

11. Finally, the last printf statement prints the value of 'd' for the last time.

Output:

Based on the above explanation, the output of the program will be:

3 1 2 2 1 3 4 4 4

Explanation of the Output:

- The first call to the 'count' function prints the value of 'n' which is 3, and the value of 'd' which is 1.
- Then, the function is recursively called with n-1=2.
- In the second call, the value of 'n' is 2, and the value of 'd' is still 1.
- The process continues until 'n' becomes 1.
- In the last call, the value of 'n' is 1, and the value of 'd' is still 1.
- After that, the function starts returning back to the previous calls.
- In the second call, the value of 'd' remains 1.
- In the first call, the value of 'd' is incremented to 2.
- Finally, the last printf statement outside the if condition prints the value of 'd' which is 4.

Hence, the output of the program is 3 1 2 2 1 3 4 4 4.
Explore Courses for Computer Science Engineering (CSE) exam
Question Description
What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following Cprogram?void count (int n) {static int d=1;printf ("%d",n);printf ("%d",d);d++;if (n>1) count (n-1);printf ("%d",d);}void main(){count (3);}a)3 1 2 2 1 3 4 4 4b)3 1 2 1 1 1 2 2 2c)3 1 2 2 1 3 4d)3 1 2 1 1 1 2Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam
Signup to solve all Doubts
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev